﻿namespace FFF.Test.Application.Repositories.FW
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Domain.Repositories;
    using Abp.EntityFrameworkCore;
    using Abp.ObjectMapping;
    using Abp.UI;
    using Ydm.Helper;
    using Ydm.Extensions;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.Repositories.FW;
    using FFF.Test.Entity.FW;
    using FFF.Test.Entity.FW.Dto;
    using FFF.Test.Entity.FW.View;

    /// <summary>
    /// 登陆日志表仓储
    /// </summary>
    public class FWLoginLogRepository :
        YdmAbpRepositoryBase<FWLoginLogEntity, Guid, FWLoginLogCreateInputDto, FWLoginLogUpdateInputDto, FWLoginLogDto, ViewFWLoginLog, ViewFWLoginLog>,
        IFWLoginLogRepository
    {
        private readonly IObjectMapper _objectMapper;

        /// <summary>
        /// Initializes a new instance of the <see cref="FWLoginLogRepository"/> class.
        /// 仓储构造方法
        /// </summary>
        /// <param name="dbContextProvider">对应数据库的对象</param>
        /// <param name="objectMapper">自动映射对象</param>
        public FWLoginLogRepository(
            IDbContextProvider<MyDbContext> dbContextProvider,
            IObjectMapper objectMapper
            )
            : base(dbContextProvider, objectMapper)
        {
            this._objectMapper = objectMapper;
        }

        /// <summary>
        /// 登陆错误的次数
        /// </summary>
        /// <param name="userCode">用户登陆的code</param>
        /// <param name="ip">用户的IO</param>
        /// <returns>返回登陆错误的次数</returns>
        public async Task<int> GetErrorCount(string userCode, string ip)
        {
            var errorCount = await this.GetAll().Where(x => x.UserCode == userCode && x.UserIP == ip && EF.Functions.DateDiffDay(x.CreateTime, DateTime.Now) == 0).CountAsync();
            return errorCount;
        }
    }
}
